package com.his.back.controller;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.his.back.pojo.*;
import com.his.back.service.MenZhenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/menZhen")
public class MenZhenController {
    @Autowired
    MenZhenService menZhenService;

    @RequestMapping("/searchPatient")
    public List<member> searchPatient(@RequestParam String content){
        //System.out.println("find ->content: "+content);
        //System.out.println("-> member: "+ menZhenService.find("%"+content+"%").toString());
        return menZhenService.searchPatient("%"+content+"%");
    }

    @RequestMapping("/getUserMemberList")
    public List<member> getUserMemberList(@RequestParam String userId, @RequestParam int stateId){
        //System.out.print("getUserMemberList->");
        //System.out.println("userId: " + userId + " stateId:" + stateId);
        //System.out.print("memberList: ");
        //System.out.println(menZhenService.getUserMemberList(userId,stateId).toString());
        return menZhenService.getUserMemberList(userId,stateId);
    }

    @RequestMapping("/getDeptMemberList")
    public List<member> getDeptMemberList(@RequestParam String deptId, @RequestParam int stateId){
        //System.out.print("getDeptMemberList->");
        //System.out.println("deptId: " + deptId + " stateId:" + stateId);
        //System.out.println("memberList: ");
        //System.out.println(menZhenService.getDeptMemberList(deptId,stateId).toString());
        return menZhenService.getDeptMemberList(deptId,stateId);
    }

    @RequestMapping("/updatePatientInfo")
    public int updatePatientInfo(@RequestBody String postInfo){
        //System.out.println(postInfo);
        JsonParser jp = new JsonParser();
        JsonObject jo = jp.parse(postInfo).getAsJsonObject();
        String patientInfo = jo.get("patientInfo").getAsString();
        String solveXi = jo.get("solveXi").getAsString();
        String solveZhong = jo.get("solveZhong").getAsString();
        //System.out.println("patientInfo:" + patientInfo);
        String patientId = jo.get("patientId").getAsString();
        //System.out.println("patientId:" + patientId);
        //System.out.println(menZhenService.updatePatientInfo(patientInfo));
        return menZhenService.updatePatientInfo(patientId,patientInfo) + menZhenService.updatePatientSolve(patientId,solveXi,solveZhong);
    }

    @RequestMapping("/getPro")
    public List<project> selectProByType(@RequestParam String projType){
        return menZhenService.selectProByType(projType);
    }

    @RequestMapping("/getProResult")
    public String selectProResult(@RequestParam String patientId, @RequestParam String projName){
        return menZhenService.selectProResult(patientId,projName);
    }

    @RequestMapping("/insertPro")
    public int updatePro(@RequestBody String postInfo){
        //System.out.println(postInfo);
        JsonParser jp = new JsonParser();
        JsonObject jo = jp.parse(postInfo).getAsJsonObject();
        String patientId = jo.get("patientId").getAsString();
        JsonArray proJNameList = jo.get("projNameList").getAsJsonArray();
        //System.out.println(patientId);
        //System.out.println(proJNameList);
        int num = 0;
        for(int i = 0; i < proJNameList.size(); i ++){
            //System.out.println((proJNameList.get(i).getAsString()));
            num += menZhenService.insertPro(patientId,proJNameList.get(i).getAsString());
        }
        return num;
    }

    @RequestMapping("/deletePro")
    public int deletePro(@RequestBody String postInfo){
        System.out.println(postInfo);
        JsonParser jp = new JsonParser();
        JsonObject jo = jp.parse(postInfo).getAsJsonObject();
        String patientId = jo.get("patientId").getAsString();
        JsonArray proJNameList = jo.get("projNameList").getAsJsonArray();
        System.out.println(patientId);
        System.out.println(proJNameList);
        int num = 0;
        for(int i = 0; i < proJNameList.size(); i ++){
            //System.out.println((proJNameList.get(i).getAsString()));
            num += menZhenService.deletePro(patientId,proJNameList.get(i).getAsString());
        }
        return num;
    }

    @RequestMapping("/getApplyTemList")
    public List<tempate> selectTemByType(@RequestParam String type){
        return menZhenService.selectTemByType(type);
    }

    @RequestMapping("/getTemPro")
    public List<project> getTemProList(@RequestParam String temName, @RequestParam String type){
        return menZhenService.getTemProList(temName,type);
    }

    @RequestMapping("/selectProCost")
    public List<procost> selectProCost(@RequestParam String patientId,@RequestParam String type){
        //System.out.println(menZhenService.selectProCost(patientId,type));
        return menZhenService.selectProCost(patientId,type);
    }

    @RequestMapping("/getXiData")
    public String selectSolveXi(@RequestParam String patientId){
        return menZhenService.selectSolveXi(patientId);
    }

    @RequestMapping("/getZhongData")
    public String selectSolveZhong(@RequestParam String patientId){
        return menZhenService.selectSolveZhong(patientId);
    }

    @RequestMapping("/getJianchaData")
    public List<Map<String,String>> selectJianchaData(@RequestParam String patientId){
        return menZhenService.selectJianchaData(patientId);
    }

    @RequestMapping("/getJianyanData")
    public List<Map<String,String>> selectJianyanData(@RequestParam String patientId){
        return menZhenService.selectJianyanData(patientId);
    }

    @RequestMapping("/updateQuezhen")
    public int updateQuezhen(@RequestBody String postInfo){
        JsonParser jp = new JsonParser();
        JsonObject jo = jp.parse(postInfo).getAsJsonObject();
        String solveXi = jo.get("solveXi").getAsString();
        String solveZhong = jo.get("solveZhong").getAsString();
        String patientId = jo.get("patientId").getAsString();
        return menZhenService.updatePatientSolve(patientId,solveXi,solveZhong);
    }

    @RequestMapping("/selectDrugByType")
    public List<drug> selectDrugByType(@RequestParam String type){
        return menZhenService.selectDrugByType(type);
    }

    @RequestMapping("/searchTemByName")
    public List<Map<String,String>> selectTemByName(@RequestParam String content, @RequestParam String type){
        return menZhenService.selectTemByName(content,type);
    }

    @RequestMapping("/searchTemContent")
    public List<drug> searchTemContent(@RequestParam String temName){
        return menZhenService.searchTemContent(temName);
    }

    @RequestMapping("/submitChufang")
    public int updateChufang(@RequestBody String postInfo){
        return menZhenService.updateChufang(postInfo);
    }

    @RequestMapping("/deleteChufang")
    public int deleteChufang(@RequestBody String postInfo){
        return menZhenService.deleteChufang(postInfo);
    }

    @RequestMapping("/zhenbi")
    public int updateMemberState(@RequestParam String patientId){
        return menZhenService.updateMemberState(patientId);
    }
}
